package com.lancoo.znbkxx.beans;

import android.app.Application;

import com.lancoo.znbkxx.base.AppConstant;
import com.lancoo.znbkxx.base.AppContext;
import com.lancoo.znbkxx.base.AppSettingsBean;
import com.lancoo.znbkxx.base.IApplicationStateCall;
import com.lancoo.znbkxx.utils.SharedPreferencesUtils;
import com.facebook.cache.disk.DiskCacheConfig;
import com.facebook.common.logging.FLog;
import com.facebook.common.util.ByteConstants;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.imagepipeline.core.ImagePipelineConfig;

import java.io.File;
import java.io.IOException;

import io.reactivex.exceptions.UndeliverableException;
import io.reactivex.functions.Consumer;
import io.reactivex.plugins.RxJavaPlugins;

/**
 * Create by TangJun on 2019/2/16
 */
public class AppInit extends IApplicationStateCall {
    @Override
    public void onCreateInit(Application application) {
        SharedPreferencesUtils.getInstance(application);
        DiskCacheConfig diskCacheConfig = DiskCacheConfig.newBuilder(application)
                .setBaseDirectoryPath(new File(AppConstant.PicCacheDir))
                .setBaseDirectoryName("picCache").setMaxCacheSize(200 * ByteConstants.MB)
                .setMaxCacheSizeOnLowDiskSpace(100 * ByteConstants.MB)
                .setMaxCacheSizeOnVeryLowDiskSpace(50 * ByteConstants.MB)
                .setMaxCacheSize(80 * ByteConstants.MB).build();
        ImagePipelineConfig config = ImagePipelineConfig.newBuilder(application)
                .setMainDiskCacheConfig(diskCacheConfig)
                .setDownsampleEnabled(true)
                .build();

        Fresco.initialize(application, config);
        FLog.setMinimumLoggingLevel(FLog.VERBOSE);

        RxJavaPlugins.setErrorHandler(new Consumer<Throwable>() {
            @Override
            public void accept(Throwable e) {
                if (e instanceof UndeliverableException) {
                    e = e.getCause();
                }
                if ((e instanceof IOException)) {
                    return;
                }
                if (e instanceof InterruptedException) {
                    return;
                }
                if ((e instanceof NullPointerException) || (e instanceof IllegalArgumentException)) {
                    Thread.currentThread().getUncaughtExceptionHandler().uncaughtException(Thread.currentThread(), e);
                    return;
                }
                if (e instanceof IllegalStateException) {
                    Thread.currentThread().getUncaughtExceptionHandler().uncaughtException(Thread.currentThread(), e);
                    return;
                }
            }
        });
        AppSettingsBean settingsBean = AppContext.getInstance(application).getSettingsBean();
        settingsBean.setBaseIp("192.168.4.49");
        settingsBean.setBasePort(10102);
        AppContext.getInstance(application).getPersistentDataCenter().saveAppSettingBeanFromSource(settingsBean);
        AppContext.getInstance(application).ReFresh();
    }

    @Override
    public void onLowMemory(Application application) {

    }
}
